/*
 * @LastEditors: yuSeen
 * @LastEditTime: 2022-01-09 17:59:32
 */
import { defineModule } from 'concent';
import { useLocalStorage } from "@/hooks/localStorage/useLocalStorage";
import { getAccountInfo } from "@/service/api/user/user";
const { getStorageItem } = useLocalStorage();

const userStore = defineModule({
    state: {
      userInfo: {
        nickname: "未登录",
        avatarUrl: "",
      },
      token: getStorageItem("token") || "",
    },
    computed: {
      // 是否已登录
      isLogin({ token }): boolean {
        return token !== "";
      },
    },
    reducer: {
      // 设置token(cookie)
      setToken(token: string) {
        return {token}
      },
      // 更新用户信息
      async updateUserInfo() {
        const { account, profile } = await getAccountInfo();
        return {
          userInfo: Object.assign(account || {}, profile || {})
        }
      },
    },
  })

export default userStore;
